package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import java.util.ArrayList;

/**
 * Created by Zizy on 2014/9/18.
 */
public class MyDatabase_SportsRecords extends MyDataBase {
    public static final String TABLE_NAME = "SportsRecords";

    public MyDatabase_SportsRecords(Context context) {
        super(context);
        initMyDatabase();
    }

    @Override
    public void initMyDatabase() {
        CreateTable();
    }

    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat, String where_expression) {
        ArrayList<Object> sportsRecordList = new ArrayList<Object>();
        FieldsSportsRecords fieldsSportsRecords = null;

        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s where %s;", selectWhat, TABLE_NAME,
                where_expression), null);
        while (cursor.moveToNext()) {
            fieldsSportsRecords  = new FieldsSportsRecords(cursor.getInt(0),
                    cursor.getInt(1), cursor.getString(2), cursor.getString(3),
                    cursor.getInt(4));

            sportsRecordList.add(fieldsSportsRecords);

        }
        cursor.close();
        return sportsRecordList;
    }

    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat) {
        ArrayList<Object> sportsRecordList = new ArrayList<Object>();
        FieldsSportsRecords fieldsSportsRecords = null;

        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s  order by id desc;", selectWhat, TABLE_NAME
                ), null);
        while (cursor.moveToNext()) {
            fieldsSportsRecords  = new FieldsSportsRecords(cursor.getInt(0),
                    cursor.getInt(1), cursor.getString(2), cursor.getString(3),
                    cursor.getInt(4));

            sportsRecordList.add(fieldsSportsRecords);

        }
        cursor.close();
        return sportsRecordList;

    }

    public void insertDatabase(Object... objects) {
        int userId=(Integer)objects[0];
        String sportsDate=(String)objects[1];
        String sportsName = (String) objects[2];
        String SportsTime = (String) objects[3];
        this.db.execSQL(String
                .format("insert into %s (UserId,SportsDate,SportsName,SportsTime) values('%s','%s','%s','%s' )",
                        TABLE_NAME, userId,sportsDate,sportsName,SportsTime));
    }


    @Override
    public void updateDatabase(Object... objects) {

    }

    @Override
    public void CreateTable() {
        try {
            String sql = "create table if not exists " + TABLE_NAME
                    + " (Id integer primary key autoincrement, "
                    + "UserId INTEGER ,"+ "SportsDate TEXT, " + "SportsName TEXT, "
                    + "SportsTime INTEGER);";
            db.execSQL(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public class FieldsSportsRecords{
        int id;
        int userId;
        String date;
        String sportsName;
        int sportsTime;

        public int getId() {
            return id;
        }

        public int getUserId() {
            return userId;
        }

        public String getSportsName() {
            return sportsName;
        }

        public int getSportsTime() {
            return sportsTime;
        }

        public String getDate() {
            return date;
        }

        public FieldsSportsRecords(int id, int userId, String date, String sportsName, int sportsTime) {
            this.id = id;
            this.userId = userId;
            this.date = date;
            this.sportsName = sportsName;
            this.sportsTime = sportsTime;
        }
    }
}
